<?php
namespace app\api\logic;
use app\api\BaseModel;
use think\Cache;
use think\Request;
/**
 * Class descript.
 * User: chen guang
 * Date: 2017/11/9 11:22
 */
class Center extends \app\api\BaseModel
{
	
	public function __construct(){
		parent::__construct();
		$this->member   = db('member');
	}
	
    /*
     *	修改个人资料
     */
	public function modify(){
    	
		$id   = $this->user['id'];
		//省
		if(isset($this->requestData['province'])){
			$province = trim($this->requestData['province']);
			$data['province'] = $province;
		}
		
		//市
		if(isset($this->requestData['city'])){
			$city = trim($this->requestData['city']);
			$data['city'] = $city;
		}
		
		//区
		if(isset($this->requestData['county'])){
			$county = trim($this->requestData['county']);
			$data['county'] = $county;
		}
		
		$update = $this->member->where("id='$id'")->update($data);
		if(!$update){
			return array('message'=>'修改失败');	
		}else{
			return array();	
		}
    }
	/*
     *	绑定手机
     */
	public function bind(){
		$id   = $this->user['id'];
		//手机号
		if(isset($this->requestData['mobile'])){
			if(isset($this->requestData['code'])){
				$code = $this->code($this->requestData['code']);
				if($code==1) return array('message'=>'验证码错误');
				if($code==2){
					$mobile = trim($this->requestData['mobile']);
					$find = $this->member->where("mobile='$mobile'")->find();
					if($find) return array('message'=>'该手机已被绑定');
					$data['mobile'] = $mobile;
					$update = $this->member->where("id='$id'")->update($data);
					if(!$update){
						return array('message'=>'绑定失败');	
					}else{
						return array();	
					}
				}else{
					return array('message'=>'绑定失败');	
				} 
			}else{
				return array('message'=>'验证码为空');
			}
		}else{
			return array('message'=>'手机号为空');
		}
    }
	/**
     * 验证验证码
     */
    public function code(){
        $code = input("code/s");
        if (base64_encode($code) != Cache::get('bind')) {
            $pd = 1;  //手机错误
        }else{
            $pd = 2;  //手机正确
        }
		return $pd;
    }
	/*
     *	会员中心
     */
	public function center(){
		(new Order())->index();
		$id   = $this->user['id'];
		$member = checkMember($id);//查询会员表
		if(!$member) return array('message'=>'用户不存在');
    	$arr['id'] = $member['id'];
		$arr['headimage'] = $member['wx_avatar'];
		$arr['nickname']  = $member['nickname'];
		$number = $member['number'];
		if(empty($number)||$number==0){
			$number = time();
			$findmember = db('member')->where("number='$number'")->find();
			if($findmember){
				$rand = rand(1, 10000);
				$number = $number-$rand;
			}
			$data['number'] = $number;
			db('member')->where("id='$id'")->update($data);
		}
		$arr['number']    = $number;
		$arr['mobile']    = $member['mobile'];
		$arr['type']      = $member['type'];
		$arr['integral']  = $member['integral'];

		$where['delete']=1;
		$where['status']=0;
		$where['uid']= $this->user['id'];
		$newscount=db("news_user")->alias("u")
				->field("u.id,n.title,n.create_time,n.des,u.status")
				->join("news n","n.id=u.nid")
				->order("n.id desc")
				->where($where)
				->count();
		$ddd['uid']= $this->user['id'];
		$ddd['status']= 0;
		$tipcount=db("tip_user")
				->order("id desc")
				->where($ddd)
				->count();
		$arr['noread']=$tipcount+$newscount;
		return $arr;
    }

	/*
 *	会员二维码
 */
	public function qrcode(){
			vendor("phpqrcode.phpqrcode");
			$url= request()->domain().'/qrcode/member'.$this->user['id'].'.png';
			$uid = $this->user['id'];
			$member = db('member')->where("id='$uid'")->find();
			$number = $member['number'];
			if(file_exists(QRCODE_PATH.'member'.$this->user['id'].'.png')){
				return array("wx_avatar"=>$this->user['wx_avatar'],"qrcode"=>$url,"nickname"=>$this->user['nickname'],"number"=>$number);
			}
			$value ='4'.$this->user['id']; //二维码内容
			$errorCorrectionLevel = 'H';//容错级别
			$matrixPointSize = 6;//生成图片大小
			//生成二维码图片
			\QRcode::png($value,QRCODE_PATH. 'member'.$this->user['id'].'.png', $errorCorrectionLevel, $matrixPointSize, 2);
		/*	$logo = ROOT_PATH.'public/logo.jpg';//准备好的logo图片
			$QR =QRCODE_PATH.'member'.$this->user['id'].'.png';//已经生成的原始二维码图
			if ($logo !== FALSE) {
				$QR = imagecreatefromstring(file_get_contents($QR));
				$logo = imagecreatefromstring(file_get_contents($logo));
				$QR_width = imagesx($QR);//二维码图片宽度
				$QR_height = imagesy($QR);//二维码图片高度
				$logo_width = imagesx($logo);//logo图片宽度
				$logo_height = imagesy($logo);//logo图片高度
				$logo_qr_width = $QR_width / 5;
				$scale = $logo_width/$logo_qr_width;
				$logo_qr_height = $logo_height/$scale;
				$from_width = ($QR_width - $logo_qr_width) / 2;
				//重新组合图片并调整大小
				imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
						$logo_qr_height, $logo_width, $logo_height);
			}
			ImagePng($QR,QRCODE_PATH.'member'.$this->user['id'].'.png');*/
			if(file_exists(QRCODE_PATH.'member'.$this->user['id'].'.png')){
				return array("wx_avatar"=>$this->user['wx_avatar'],"qrcode"=>$url,"nickname"=>$this->user['nickname'],"number"=>$number);
			}
	}
	/*
 *	tip
 */
	public function tip()
	{
		$where['delete']=1;
		$where['status']=0;
		$where['uid']= $this->user['id'];
		$newscount = db("news_user")->alias("u")
				->field("u.id,n.title,n.create_time,n.des,u.status")
				->join("news n", "n.id=u.nid")
				->order("n.id desc")
				->where($where)
				->count();
		$ddd['uid'] = $this->user['id'];
		$ddd['status'] = 0;
		$tipcount = db("tip_user")
				->order("id desc")
				->where($ddd)
				->count();
		return array(
			"order"=>$tipcount,
			"system"=>$newscount
		);
	}
	/*
 *	系统信息
 */
	public function system(){
		$p=isset($this->requestData['page'])?$this->requestData['page']:'1';
		$size=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
		$where['delete']=1;
		$where['uid']= $this->user['id'];
		$news=db("news_user")->alias("u")
				->field("u.id,n.title,n.create_time,n.des,u.status")
				->join("news n","n.id=u.nid")
				->order("n.id desc")
				->page($p,$size)
				->where($where)
				->select();
		foreach($news as &$v){
			$v['create_time']=date("Y-m-d",$v['create_time']);
		}
		return $news;
	}
	/*
 *	系统信息设置已读
 */
	public function read(){
		$where['uid']= $this->user['id'];
		$where['id']= $this->requestData['id'];
		$news=db("news_user")->where($where)->find();
		if(!$news) return array('message'=>'记录不存在');
		$res=db("news_user")->where($where)->update(array("status"=>1));
		return array();
	}
	/*
 *	订单优惠券信息
 */
	public function information(){
		$p=isset($this->requestData['page'])?$this->requestData['page']:'1';
		$size=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
		$where['uid']= $this->user['id'];
		$news=db("tip_user")
				->order("id desc")
				->page($p,$size)
				->where($where)
				->select();
		foreach($news as &$v){
			$v['create_time']=date("Y-m-d",$v['create_time']);
			$v['image']=IMG_PATH.$v['image'];
		}
		return $news;
	}
	/*
*	订单优惠券信息已读
*/
	public function reader(){
		$where['uid']= $this->user['id'];
		$where['id']= $this->requestData['id'];
		$news=db("tip_user")->where($where)->find();
		if(!$news) return array('message'=>'记录不存在');
		$res=db("tip_user")->where($where)->update(array("status"=>1));
		return array();
	}
	/*
*	订单优惠券信息s删除
*/
	public function delete(){
		$where['uid']= $this->user['id'];
		$where['id']= $this->requestData['id'];
		$news=db("tip_user")->where($where)->find();
		if(!$news) return array('message'=>'记录不存在');
		$res=db("tip_user")->where($where)->delete();
		if(!$res) return array('message'=>'删除失败');
		return array();
	}
	/*
*	系统信息设置删除
*/
	public function del(){
		$where['uid']= $this->user['id'];
		$where['id']= $this->requestData['id'];
		$news=db("news_user")->where($where)->find();
		if(!$news) return array('message'=>'记录不存在');
		$res=db("news_user")->where($where)->delete();
		if(!$res) return array('message'=>'删除失败');
		return array();
	}

	/**
	 * 系统信息详情
	 */
	public function newsdetail(){
		$where['u.id']=input('id');
		$news=db("news_user")->alias("u")
				->field("u.id,n.title,n.create_time,n.content,n.des,u.status")
				->join("news n","n.id=u.nid")
				->order("n.id desc")
				->where($where)
				->find();
		$news['create_time']=date("Y-m-d H:i:s",$news['create_time']);
	//	$news['image']=IMG_PATH.$news['image'];
	   return $news;
	}

}

?>